﻿using SCM.Common;
using SCM.Common.Extensions;
using SCM.Model.DBEntity;
using SCM.Servie.IBusiness;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace SCM.Servie.Business 
{
    /// <summary>
    /// 供应商联系人服务
    /// </summary>
    public class SupplierContactService : ISupplierContactService
    {
        private readonly DbContext dal = new DbContext();

        public string Add(T_Supplier_Contact model)
        {
            return dal.Insertable(model).ExecuteReturnEntity().ID;
        }

        public bool Delete(string supplierId)
        {
            //return dal.Updateable<T_Supplier_Contact>(new
            //{
            //    DataStatus = DataStatusEnum.Deleted,
            //    ModifyTime = DateTime.Now,
            //}).Where(p => ids.Contains(p.ID)).ExecuteCommand() > 0;
            return dal.Deleteable<T_Supplier_Contact>(p => p.SupplierID == supplierId).ExecuteCommand() > 0;
        }

        public T_Supplier_Contact GetById(string id)
        {
            return dal.Get<T_Supplier_Contact>(id);
        }

        public List<T_Supplier_Contact> GetList(string supplierID)
        {
            Expression<Func<T_Supplier_Contact, bool>> whereExp = (a) => a.SupplierID == supplierID;
            return dal.Queryable<T_Supplier_Contact>().Where(whereExp).ToList();
        }

        public PageData<T_Supplier_Contact> Query(string supplierID, string contacter, PageInfo page)
        {
            int count = 0;
            Expression<Func<T_Supplier_Contact, bool>> whereExp = (a) => true;
            if (!string.IsNullOrWhiteSpace(supplierID))
            {
                whereExp = whereExp.Compose((a) => a.SupplierID == supplierID, Expression.AndAlso);
            }
            if (!string.IsNullOrWhiteSpace(contacter))
            {
                whereExp = whereExp.Compose((a) => a.Contacter.StartsWith(contacter), Expression.AndAlso);
            }

            List<T_Supplier_Contact> list = dal.Queryable<T_Supplier_Contact>().Where(whereExp).OrderBy(a => a.TelPhone, SqlSugar.OrderByType.Desc).ToPageList(page.PageIndex, page.PageSize, ref count);
            page.DataCount = count;
            return new PageData<T_Supplier_Contact> { Data = list, Page = page };
        }

        public bool Update(T_Supplier_Contact model)
        {
            return dal.Modify(model);
        }

        public bool Save(List<T_Supplier_Contact> list)
        {
            return dal.SaveOrUpdate(list) > 0;
        }
    }
}
